// State.cpp
#include "stdafx.h"
#include "State.h"
#include "StateTransition.h"

State::State()
{
	m_Parent=0;
	m_Child=0;
	m_CostByParent=INT_MAX;
	m_CostByChild=INT_MAX;
	m_bOpenedByParent=false;
	m_bClosedByParent=false;
	m_bOpenedByChild=false;
	m_bClosedByChild=false;
}
//-------------------------------
State::State(Line* Line, Station* Station)
{
	m_Parent=0;
	m_Child=0;
	m_CostByParent=INT_MAX;
	m_CostByChild=INT_MAX;
	m_Line=Line;
	m_Station=Station;
	m_bOpenedByParent=false;
	m_bClosedByParent=false;
	m_bOpenedByChild=false;
	m_bClosedByChild=false;
}
//-------------------------------
void State::Reset()
{
	m_Parent=0;
	m_Child=0;
	m_CostByParent=INT_MAX;
	m_CostByChild=INT_MAX;
	m_bOpenedByParent=false;
	m_bClosedByParent=false;
	m_bOpenedByChild=false;
	m_bClosedByChild=false;
}
//-------------------------------
int State::GetTransitionCost(State* NextState, bool bIsFirstStation)
{
	for(auto it=m_SuccessorTransitions.begin(); it!=m_SuccessorTransitions.end(); it++)
	{
		if((*it)->m_StateA==NextState || (*it)->m_StateB==NextState)
		{
			if(bIsFirstStation && (*it)->m_StateA->m_Station==(*it)->m_StateB->m_Station)
				return 0;
			return (*it)->m_Cost;
		}
	}
	return INT_MAX;
}
//-------------------------------
bool State::IsClosed(bool bBackward)
{
	return bBackward?m_bClosedByChild:m_bClosedByParent;
}
//-------------------------------
bool State::IsOpened(bool bBackward)
{
	return bBackward?m_bOpenedByChild:m_bOpenedByParent;
}
//-------------------------------
int State::GetCost(bool bBackward)
{
	return bBackward?m_CostByChild:m_CostByParent;
}
//-------------------------------
void State::SetClosed(bool bClosed, bool bBackward)
{
	if(bBackward)
		m_bClosedByChild=bClosed;
	else
		m_bClosedByParent=bClosed;
}
//-------------------------------
void State::SetOpened(bool bClosed, bool bBackward)
{
	if(bBackward)
		m_bOpenedByChild=bClosed;
	else
		m_bOpenedByParent=bClosed;
}
//-------------------------------
void State::SetCost(int Cost, bool bBackward)
{
	if(bBackward)
		m_CostByChild=Cost;
	else
		m_CostByParent=Cost;
}
//-------------------------------
void State::SetParent(State* Parent, bool bBackward)
{
	if(bBackward)
		m_Child=Parent;
	else
		m_Parent=Parent;
}